/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.beans.bind;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;

import com.bluemartini.dna.BusinessObject;
import com.bluemartini.dna.DNAList;

/**
 * Provides a simple binding service which delegates the actual bean
 * and business object binding to its registered data binders.
 * 
 * @author Scott Rossillo
 *
 */
public class SimpleBindingService extends AbstractBindingService
		implements BindingService, InitializingBean {
	
	private final static Log log = LogFactory.getLog(SimpleBindingService.class);

	private FieldPropertyMappingHandler fieldPropertyMappingHandler;
	
	private BeanBinder beanDataBinder;
	private BusinessObjectBinder businessObjectBinder;
	
	/**
	 * Creates a new simple binding service.
	 */
	public SimpleBindingService() { }
	
	/**
	 * Guarantees that a bean data binder and business object binder have been
	 * registered.
	 * 
	 * @throws IllegalArgumentException if this bean's <code>beanDataBinder</code>
	 * or <code>businessObjectBinder</code> properties are not set
	 * 
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 * 
	 */
	public void afterPropertiesSet() throws Exception {
		
		if(fieldPropertyMappingHandler == null) {
			throw new IllegalArgumentException("[FieldPropertyMapper] property must be set!");
		}
		
		this.beanDataBinder = new SimpleBeanBinder(fieldPropertyMappingHandler);
		this.businessObjectBinder = new SimpleBusinessObjectBinder(fieldPropertyMappingHandler);
		
		
	}

	/**
	 * Delegates bean binding the the registered bean data binder.
	 * 
	 * @see net.martinimix.beans.bind.BindingService#bindBean(com.bluemartini.dna.DNAList, java.lang.Object)
	 */
	public final Object bindBean(DNAList source, Object target) {
		
		if(log.isDebugEnabled()) {
			log.debug("Binding [" + source + "] to [" + target + "] fields. ");
		}
		
		return beanDataBinder.bind(source, target);
	}

	/* (non-Javadoc)
	 * @see net.martinimix.dao.bind.BindingService#bindBusinessObject(java.lang.Object, java.lang.String)
	 */
	public final BusinessObject bindBusinessObject(Object source, String type)
			throws BusinessObjectBindException {

		if(log.isDebugEnabled()) {
			log.debug("Binding [" + source + "] to [" + type + "]");
		}

		return businessObjectBinder.bind(source, type);
	}

	public void setFieldPropertyMappingHandler(
			FieldPropertyMappingHandler fieldPropertyMappingHandler) {
		this.fieldPropertyMappingHandler = fieldPropertyMappingHandler;
	}

	
	
}
